package com.xingkeduo.websocket.hanler;

import com.xingkeduo.util.ScreenMapUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;


/**
 * @author chi  2019-05-24 17:46
 **/
@Component
@ServerEndpoint("/websocket/transfer/{storeId}/{identificationCode}")
@Slf4j
public class TransferHandler {

    @Resource
    private ScreenMapUtil screenMapUtil;

    @OnOpen
    public void onOpen(@PathParam("storeId") String storeId
            ,@PathParam("identificationCode") String identificationCode, Session session) {

        log.info("传输连接开启:{},{}", storeId, identificationCode);

        screenMapUtil.putStore(identificationCode, storeId, session);

    }

    @OnError
    public void onError(@PathParam("storeId") String storeId
            ,@PathParam("identificationCode") String identificationCode, Session session, Throwable error) {
        log.error("{},{} 传输连接服务端发生了错误:{}",storeId, identificationCode, error);
    }

    /**
     * 连接关闭
     */
    @OnClose
    public void onClose(@PathParam("storeId") String storeId
            ,@PathParam("identificationCode") String identificationCode, Session session) {
        screenMapUtil.removeStore(identificationCode,storeId);
        log.error("传输连接关闭:{}", identificationCode);
    }

    /**
     * 收到客户端的消息
     *
     * @param message 消息
     * @param session 会话
     */
    @OnMessage
    public void onMessage(@PathParam("storeId") String storeId, @PathParam("identificationCode") String identificationCode, String message, Session session) {
        log.info("传输连接来自客户端消息：" + message + "客户端的id是：" + session.getId());
    }


}
